#!/usr/bin/perl

#    D-HUD D-Star Repeater and Reflector Heads Up Display
#    Copyright (C) 2011  Andrew Ragusa (KC9EVU) and Corey Shields (KB9JHU)
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#    ajragusa@gmail.com cshields@gmail.com


use strict;
use CGI;
use Template;

my %methods;
our $tt;

if(!defined($tt)){
    $tt = Template->new(INCLUDE_PATH => '/home/aragusa/d-hud/www/') or warn $Template::ERROR;

    my $expires;
    my $debug=0;
    if($debug){
	$expires = "-1d";
    }else{
	$expires = "+10d";
    }
    
    my $base_path = "/home/aragusa/d-hud/www/";
    
    my $vars;
    $vars->{'userid'} = $ENV{'REMOTE_USER'};
    
    sub home_page{
	$vars->{'body_template'} = "overview.html";
	$vars->{'title'} = "Overview";
	my $output;
	
	my $res = $tt->process('page_template.html',$vars,\$output) || warn $tt->error();
	return $output;
    }
    
    $methods{'home_page'} = \&home_page;
    
    sub log_page{
	
	$vars->{'body_template'} = "log.html";
	$vars->{'title'} = "Log";
	my $output;
	
	my $res = $tt->process('page_template.html',$vars,\$output) || warn $tt->error();
	return $output;
    }
    
    $methods{'log'} = \&log_page;
    
    
    sub user_page{
	$vars->{'body_template'} = "users.html";
	$vars->{'title'} = "Users";
	my $output;
	
	my $res = $tt->process('page_template.html',$vars,\$output) || warn $tt->error();
	return $output;
	
    }

    $methods{'users'} = \&user_page;
    

    sub report_page{
	$vars->{'body_template'} = "reports.html";
	$vars->{'title'} = "Reports";
	my $output;
	
	my $res = $tt->process('page_template.html',$vars,\$output) || warn $tt->error();
	return $output;
	
    }
    
    $methods{'reports'} = \&report_page;
    
    sub help{
	
    }
    
    $methods{'help'} = \&help;
}

    
sub main{
    my $results;
    my $cgi = new CGI;
    my $method = $cgi->param("method");
    if(!defined($method)){
	$results = $methods{'home_page'}();
    }elsif (!exists $methods{$method}){
	$results = "Unknown method - '$method'.";
    }else{
	$results = $methods{$method}();
    }
    
    print $cgi->header(-type=>"text/html");
    
    print $results;
    
}

main();
